package javeriana.edu.co.acceleo.Util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.Map.Entry;


public class Utils {
	public static HashMap<String,String> entities=new HashMap<>();
	public static HashMap<String, String> map = new HashMap<>();
	public static HashMap<String,HashMap<String,String>> useCases = new HashMap<>();
	public static HashMap<String,HashMap<String,String>> useCasesAction = new HashMap<>();
	public static HashMap<String,HashMap<String,String>> useCaseCondition = new HashMap<>();
	public static String path = "C:\\Users\\MarioAlonso\\workspace\\javeriana.edu.co.acceleo.webgenerator.m2t";
	public Integer insert(String entity){
		if(entities.containsKey(entity)==false){
			entities.put(entity, entity);
		}
		return entities.size();
	}
	public Integer insertMap(String useCase){
		if(map.containsKey(useCase)==false){
			map.put(useCase, useCase);
			return 0;
		}
		
			return 1;	
	}
	public Integer size(String useCase){
		
			return map.size();	
	}
	public Integer insertUseCases(String useCase){
		if(useCases.containsKey(useCase)==false){
			HashMap<String, String> mapAux = new HashMap<>();
			HashMap<String, String> mapAux2 = new HashMap<>();
			HashMap<String, String> mapAux3 = new HashMap<>();
			useCases.put(useCase, mapAux);
			useCasesAction.put(useCase, mapAux2);
			useCaseCondition.put(useCase, mapAux3);
		}
		return useCases.size();
	}
	public Integer insertEntities(String useCase,String entity){
		HashMap<String, String> mapAux = useCases.get(useCase);
		if(mapAux.containsKey(entity)==false){
			String aux = Character.toLowerCase(entity.charAt(0)) + (entity.length() > 1 ? entity.substring(1) : "");
			String aux2 = Character.toUpperCase(entity.charAt(0)) + (entity.length() > 1 ? entity.substring(1) : "");
			String value="	private "+aux2+" "+aux+" = new "+aux2+"();\n	private List<"+aux2+"> "+aux+"s = new ArrayList<"+aux2+">();\n";
			value+=" 	public "+aux2+" get"+aux2+"(){\n		return this."+aux+";\n	}\n 	public List<"+aux2+"> get"+aux2+"s(){\n		return this."+aux+"s;\n	}\n";
			mapAux.put(entity, value);			
		}
		return mapAux.size();
	}
	public Integer insertActions(String useCase,String action,String condition){
		HashMap<String, String> mapAux = useCasesAction.get(useCase);
		HashMap<String, String> mapAux2 = useCaseCondition.get(useCase);
		if(mapAux.containsKey(action)==false){
			//String value="private jpa.entities."+action+" "+action.toLowerCase()+" = new jpa.entities."+action+"();\n";
			mapAux.put(action, condition);					
		}
		if(mapAux2.containsKey(condition)==false){
			//String value="private jpa.entities."+action+" "+action.toLowerCase()+" = new jpa.entities."+action+"();\n";
			String aux = Character.toLowerCase(condition.charAt(0)) + (condition.length() > 1 ? condition.substring(1) : "");
			String aux2 = Character.toUpperCase(condition.charAt(0)) + (condition.length() > 1 ? condition.substring(1) : "");
			String value=" 	private boolean "+aux+"=false;\n";
			value+=" 	public boolean is"+aux2+"(){\n		return this."+aux+";\n	}\n";
			mapAux2.put(condition, value);
			
		}		
		return mapAux.size();
	}
	public Double div(Double r1, Double r2){
		return r1/r2;
	}
	public String generateEntites(String useCase){
		String values="";
		HashMap<String,String> mapAux = useCases.get(useCase);
		for (Entry<String, String> entry : mapAux.entrySet()) {
		    String value = entry.getValue();
		    values+=value;
		}
		return values;
	}
	public String generateActions(String useCase){
		String values="";
		HashMap<String,String> mapAux = useCasesAction.get(useCase);
		for (Entry<String, String> entry : mapAux.entrySet()) {
		    String action = entry.getKey();
		    String condition = entry.getValue();
		    if(condition.equalsIgnoreCase("null")==false){
		    	String aux = Character.toLowerCase(useCase.charAt(0)) + (useCase.length() > 1 ? useCase.substring(1) : "");
				String value=" 	public String "+action+"(){\n		"+aux+"EJB."+action+"();\n"+"		//TO-DO change the variable "+condition+"by yours business logic\n		"+condition+"=false;\n"+"		return \"void\";\n	}\n";
			    values+=value;
		    }
		    else{
		    	String aux = Character.toLowerCase(useCase.charAt(0)) + (useCase.length() > 1 ? useCase.substring(1) : "");
				String value=" 	public String "+action+"(){\n		"+aux+"EJB."+action+"();\n		return \"void\";\n	}\n";
			    values+=value;
		    }
		    
		}
		return values;
	}
	public String generateConditions(String useCase){
		String values="";
		HashMap<String,String> mapAux = useCaseCondition.get(useCase);
		for (Entry<String, String> entry : mapAux.entrySet()) {
			String condition = entry.getKey();
			if(condition.equalsIgnoreCase("null")==false){
				String value = entry.getValue();
			    values+=value;
			}
		    
		}
		return values;
	}
	public String generateActions2(String useCase){
		String values="";
		HashMap<String,String> mapAux = useCasesAction.get(useCase);
		for (Entry<String, String> entry : mapAux.entrySet()) {
			String action = entry.getKey();
			String value=" 	public String "+action+"(){\n		return \"void\";\n	}\n";
		    values+=value;
		}
		return values;
	}
	public String generateImports(String useCase){
		String values="";
		HashMap<String,String> mapAux = useCases.get(useCase);
		for (Entry<String, String> entry : mapAux.entrySet()) {
			String entity = entry.getKey();
			String aux = Character.toUpperCase(entity.charAt(0)) + (entity.length() > 1 ? entity.substring(1) : "");
			String value="import jpa.entities."+aux+";\n";
		    values+=value;
		}
		return values;
	}
	public String generateSetGet(){
		String values="";
		for (Entry<String, String> entry : entities.entrySet()) {
		    String value = entry.getValue();
		    //values+="	public void set"+value+"(jpa.entities."+value+" "+value.toLowerCase()+"){\n		this."+value.toLowerCase()+" = "+value.toLowerCase()+";\n	}\n";
		    values+=" 	public jpa.entities."+value+" get"+value+"(){\n		return this."+value.toLowerCase()+";\n	}\n";
		}
		return values;
	}
	public String textOfFile (String homePage,boolean flag,String directory)  throws Exception{
	    byte[] buff = new byte[512];
	    ByteArrayOutputStream outbyte = new ByteArrayOutputStream();
	    String text = null;
	    try {	      
	      //System.out.println("textOfFile- path=" + path);
	      FileInputStream in = new FileInputStream(path+directory);
	      for(int readSize = in.read(buff); readSize >= 0; readSize = in.read(buff)) {
	        outbyte.write(buff, 0, readSize);
	      }
	      in.close();
	      text = outbyte.toString();
	      //backup(fileName, text);
	    }
	    catch (Exception e){
	      System.out.println("Exception textOfFile: " 
	        + path + ": " +   e);
	    }
	    if(flag){
	    	text = text.replace("$", homePage);
	    }	    
	    return text;
	  }
	
	public void copyToGenerated (String projectName, String outputText, String directory) throws Exception{
		try {
			// create output directory and file:
			//File outdir = new File("../../genTemplates");
			//boolean outdirCreated = outdir.mkdirs();
			FileWriter writer = new FileWriter(path+"\\src-gen\\"  + projectName + directory);
			writer.write(outputText);
			writer.flush();
			writer.close();
		}
		catch (Exception e){
			System.out.println("Exception with template in genTemplates:  web.xml : " +   e);
		}
	}
	public Boolean webFileGenerate(String homePage, String projectName){		
		try {
			String text = this.textOfFile(homePage,true,"\\support-files\\web.xml");
			this.copyToGenerated(projectName, text,"\\web\\WEB-INF\\web.xml");
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	public String cssGenerate(String projectName){		
		try {
			File outdir = new File(path+"\\src-gen\\"+projectName+"\\web\\resources\\css");
			outdir.mkdirs();
			String text = this.textOfFile("",false,"\\support-files\\cssLayout.css");
			this.copyToGenerated(projectName, text,"\\web\\resources\\css\\cssLayout.css");
			text = this.textOfFile("",false,"\\support-files\\default.css");
			this.copyToGenerated(projectName, text,"\\web\\resources\\css\\default.css");
			return path+"\\src-gen\\"+projectName+"\\web\\resources";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "HOLA";
	}


}
